//
//  TFScoreButton.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/3.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

public class TFScoreButton: UIButton {
    //动画view
    @objc public let animationImage = UIImageView()
    //是否在动画中
    @objc public var isAnimation:Bool = false
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.isUserInteractionEnabled = false
        
     
        self.animationImage.backgroundColor = UIColor.clear
        
        self.addSubview(self.animationImage)
        self.clipsToBounds = false
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        if (self.isAnimation==false) {
            self.titleLabel!.isHidden = false
            let imageViewH = self.frame.size.height*0.6;
            let imageViewX = self.frame.size.width*0.1;
            let imageViewY = (self.frame.size.height - imageViewH)/2;
            
            self.imageView!.frame = CGRect(x: imageViewX, y: imageViewY, width: imageViewH, height: imageViewH)
            
            
      
            
            let titleLabelH =  self.frame.size.height*0.8
            let titleLabelX  = imageViewX + imageViewH;
            let titleLabelY = (self.frame.size.height - titleLabelH)/2;
            
            self.titleLabel!.frame = CGRect(x: titleLabelX, y: titleLabelY, width: (self.frame.size.width*0.5), height: titleLabelH)
         
        }else{
            self.titleLabel!.isHidden = true
        }
    }

    @objc public func animation()
    {
        // 回到主线程更新UI
        DispatchQueue.main.async {
               //code
            
            self.isAnimation = true
            
            let imageViewH = self.frame.size.height*0.6
            let imageViewX = self.frame.size.width*0.1
            let imageViewY = (self.frame.size.height - imageViewH)/2-(imageViewH)
            
            self.animationImage.frame = CGRect(x: imageViewX-(imageViewH*0.5), y: imageViewY, width: imageViewH*3, height: imageViewH*3)
           
            
            self.titleLabel!.isHidden = true
          
            self.animationImage.image = UIImage.init(named: "HTLive.bundle/smalljiangbei")
            self.setImage(UIImage.init(), for: .normal)
 
            UIView.animate(withDuration: 3, delay: 0.0, options: UIView.AnimationOptions.allowAnimatedContent) {
                self.animationImage.frame = CGRect(x: imageViewX, y: imageViewY, width: imageViewH, height: imageViewH)
            } completion: { Bool in
                
            }
        }
       
       
            
          
        

       
        
    }
    @objc public func setop_Animation()
    {
        self.titleLabel!.isHidden = false;
        self.animationImage.frame = self.imageView!.frame
        self.animationImage.image = UIImage.init()
        self.setImage(UIImage.init(named: "HTLive.bundle/smalljiangbei"), for: .normal)
        
        self.isAnimation = false
    }
}
